import 'package:chatgreat/bloc/account_bloc.dart';
import 'package:chatgreat/bloc/bloc_manager.dart';
import 'package:chatgreat/bloc/free_count_bloc.dart';
import 'package:chatgreat/bloc/gallery_bloc.dart';
import 'package:chatgreat/bloc/room_bloc.dart';
import 'package:chatgreat/bloc/version_bloc.dart';
import 'package:chatgreat/helper/ability.dart';
import 'package:chatgreat/helper/constant.dart';
import 'package:chatgreat/lang/lang.dart';
import 'package:chatgreat/page/chat/home_help.dart';
import 'package:chatgreat/page/chat/home_speeh_chat.dart';
import 'package:chatgreat/page/component/chat/message_state_manager.dart';
import 'package:chatgreat/page/creative_island/gallery/gallery_home.dart';
import 'package:chatgreat/page/creative_island/gallery/gallery_start_create.dart';
import 'package:chatgreat/page/guide/view.dart';
import 'package:chatgreat/page/setting/feed_back_page.dart';
import 'package:chatgreat/page/setting/my_collection_page.dart';
import 'package:chatgreat/page/setting/signout_page.dart';
import 'package:chatgreat/page/web/web_page.dart';
import 'package:chatgreat/repo/api/creative.dart';
import 'package:chatgreat/repo/chat_message_repo.dart';
import 'package:chatgreat/repo/creative_island_repo.dart';
import 'package:chatgreat/repo/settings_repo.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:chatgreat/widgets/flutter_life_state/src/observer_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localization/flutter_localization.dart';
import 'package:go_router/go_router.dart';

import 'package:chatgreat/bloc/admin_payment_bloc.dart';
import 'package:chatgreat/bloc/admin_room_bloc.dart';
import 'package:chatgreat/bloc/channel_bloc.dart';
import 'package:chatgreat/bloc/model_bloc.dart';
import 'package:chatgreat/bloc/user_bloc.dart';
import 'package:chatgreat/page/admin/channels.dart';
import 'package:chatgreat/page/admin/channels_add.dart';
import 'package:chatgreat/page/admin/channels_edit.dart';
import 'package:chatgreat/page/admin/dashboard.dart';
import 'package:chatgreat/page/admin/messages.dart';
import 'package:chatgreat/page/admin/models.dart';
import 'package:chatgreat/page/admin/models_add.dart';
import 'package:chatgreat/page/admin/models_edit.dart';
import 'package:chatgreat/page/admin/payments.dart';
import 'package:chatgreat/page/admin/recently_messages.dart';
import 'package:chatgreat/page/admin/rooms.dart';
import 'package:chatgreat/page/admin/user.dart';
import 'package:chatgreat/page/admin/users.dart';
import 'package:chatgreat/page/creative_island/draw/artistic_wordart.dart';
import 'package:chatgreat/page/balance/quota_usage_details.dart';
import 'package:chatgreat/page/creative_island/draw/artistic_qr.dart';
import 'package:chatgreat/page/setting/account_security.dart';
import 'package:chatgreat/page/app_scaffold.dart';
import 'package:chatgreat/page/lab/avatar_selector.dart';
import 'package:chatgreat/page/setting/article.dart';
import 'package:chatgreat/page/setting/background_selector.dart';
import 'package:chatgreat/page/setting/bind_phone_page.dart';
import 'package:chatgreat/page/setting/change_password.dart';
import 'package:chatgreat/page/chat/home_chat.dart';
import 'package:chatgreat/page/chat/home.dart';
import 'package:chatgreat/page/chat/home_chat_history.dart';
import 'package:chatgreat/page/chat/room_create.dart';
import 'package:chatgreat/page/component/random_avatar.dart';
import 'package:chatgreat/page/component/transition_resolver.dart';
import 'package:chatgreat/page/creative_island/my_creation.dart';
import 'package:chatgreat/page/creative_island/my_creation_item.dart';
import 'package:chatgreat/page/setting/custom_home_models.dart';
import 'package:chatgreat/page/balance/free_statistics.dart';
import 'package:chatgreat/page/chat/group/chat.dart';
import 'package:chatgreat/page/chat/group/create.dart';
import 'package:chatgreat/page/chat/group/edit.dart';
import 'package:chatgreat/page/lab/creative_models.dart';
import 'package:chatgreat/page/setting/destroy_account.dart';
import 'package:chatgreat/page/setting/diagnosis.dart';
import 'package:chatgreat/page/creative_island/draw/draw_list.dart';
import 'package:chatgreat/page/creative_island/draw/draw_create.dart';
import 'package:chatgreat/page/creative_island/draw/image_edit_direct.dart';
import 'package:chatgreat/page/lab/draw_board.dart';
import 'package:chatgreat/page/creative_island/gallery/gallery.dart';
import 'package:chatgreat/page/creative_island/gallery/gallery_item.dart';
import 'package:chatgreat/page/setting/notification.dart';
import 'package:chatgreat/page/setting/openai_setting.dart';
import 'package:chatgreat/page/balance/payment.dart';
import 'package:chatgreat/page/lab/prompt.dart';
import 'package:chatgreat/page/balance/quota_usage_statistics.dart';
import 'package:chatgreat/page/login/signin_screen.dart';
import 'package:chatgreat/page/balance/payment_history.dart';
import 'package:chatgreat/page/setting/retrieve_password_screen.dart';
import 'package:chatgreat/page/login/signup_screen.dart';
import 'package:chatgreat/page/lab/user_center.dart';
import 'package:chatgreat/page/setting/user_api_keys.dart';
import 'package:chatgreat/bloc/notify_bloc.dart';
import 'package:chatgreat/page/chat/room_edit.dart';
import 'package:chatgreat/page/chat/room_chat.dart';
import 'package:chatgreat/page/chat/rooms.dart';
import 'package:chatgreat/page/setting/setting_screen.dart';
import 'package:chatgreat/bloc/background_image_bloc.dart';
import 'package:chatgreat/bloc/chat_chat_bloc.dart';
import 'package:chatgreat/bloc/creative_island_bloc.dart';
import 'package:chatgreat/bloc/group_chat_bloc.dart';
import 'package:chatgreat/bloc/payment_bloc.dart';
import 'package:chatgreat/bloc/user_api_keys_bloc.dart';
import 'package:intl/intl.dart';

import 'page/creative_island/draw/create_generate.dart';
import 'page/creative_island/gallery/gallery_integral.dart';
import 'page/splash/splash_page.dart';

GoRouter getRouter(
    SettingRepository settingRepo,
    CreativeIslandRepository creativeIslandRepo,
    ChatMessageRepository chatMsgRepo,
    MessageStateManager messageStateManager,
    GlobalKey<NavigatorState> shellNavigatorKey,
    GlobalKey<NavigatorState> rootNavigatorKey,
    FreeCountBloc freeCountBloc,
    RoomBloc chatRoomBloc,
    GalleryBloc galleryBloc,
    AccountBloc accountBloc,
    VersionBloc versionBloc) {
  return GoRouter(
      // initialLocation: shouldLogin ? '/login' : Ability().homeRoute,
      initialLocation: '/splash',
      observers: [
        BotToastNavigatorObserver(),
        lifeFouteObserver,
      ],
      navigatorKey: rootNavigatorKey,
      routes: [
        StatefulShellRoute.indexedStack(
          // navigatorKey: _shellNavigatorKey,
          //parentNavigatorKey: _rootNavigatorKey,
          builder: (
            BuildContext context,
            GoRouterState state,
            StatefulNavigationShell navigationShell,
          ) {
            return AppScaffold(
              settingRepo: settingRepo,
              navigationShell: navigationShell,
            );
          },
          branches: [
            StatefulShellBranch(navigatorKey: shellNavigatorKey, routes: [
              GoRoute(
                name: 'chat_chat',
                path: '/chat-chat',
                pageBuilder: (context, state) => transitionResolver(
                  MultiBlocProvider(
                    providers: [
                      BlocProvider(
                          create: (context) => ChatChatBloc(chatMsgRepo)),
                      BlocProvider.value(value: freeCountBloc),
                      BlocProvider.value(value: chatRoomBloc),
                      BlocProvider(create: (context) => NotifyBloc()),
                      BlocProvider.value(
                        value: ChatBlocManager().getBloc(
                          chatHomeRoomId,
                        ),
                      ),
                    ],
                    child: HomePage(
                      stateManager: messageStateManager,
                      setting: settingRepo,
                      showInitialDialog:
                          state.queryParameters['show_initial_dialog'] ==
                              'true',
                      reward:
                          int.tryParse(state.queryParameters['reward'] ?? '0'),
                    ),
                  ),
                ),
              ),
            ]),
            StatefulShellBranch(routes: [
              GoRoute(
                name: 'creative-draw',
                path: '/creative-draw',
                pageBuilder: (context, state) => transitionResolver(
                  MultiBlocProvider(
                    providers: [
                      BlocProvider(
                          create: (context) =>
                              CreativeIslandBloc(creativeIslandRepo)),
                    ],
                    child: DrawListScreen(
                      setting: settingRepo,
                    ),
                  ),
                ),
              ),
            ]),
            StatefulShellBranch(routes: [
              GoRoute(
                name: 'creative-gallery',
                path: '/creative-gallery',
                pageBuilder: (context, state) => transitionResolver(
                  MultiBlocProvider(
                    providers: [
                      BlocProvider.value(value: galleryBloc),
                    ],
                    child: GalleryHomeScreen(
                      setting: settingRepo,
                    ),
                  ),
                ),
              ),
            ]),
            StatefulShellBranch(routes: [
              GoRoute(
                name: 'characters',
                path: '/',
                pageBuilder: (context, state) => transitionResolver(
                  MultiBlocProvider(
                    providers: [BlocProvider.value(value: chatRoomBloc)],
                    child: RoomsPage(setting: settingRepo),
                  ),
                ),
              ),
            ]),
            StatefulShellBranch(routes: [
              GoRoute(
                name: 'setting',
                path: '/setting',
                pageBuilder: (context, state) => transitionResolver(
                  MultiBlocProvider(
                    providers: [
                      BlocProvider.value(value: accountBloc),
                    ],
                    child: SettingScreen(
                        settings: context.read<SettingRepository>()),
                  ),
                ),
              ),
            ]),
          ],
        ),

        GoRoute(
          path: '/splash',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: versionBloc),
              ],
              child: SplashPage(settingRepo: settingRepo),
            ),
          ),
        ),
        GoRoute(
          path: '/guide',
          pageBuilder: (context, state) => transitionResolver(
            const GuidePage(),
          ),
        ),
        GoRoute(
          path: '/login',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: versionBloc),
              ],
              child: SignInScreen(
                settings: settingRepo,
                username: state.queryParameters['username'],
              ),
            ),
          ),
        ),

        GoRoute(
          name: 'setting-collection',
          path: '/setting/collection',
          pageBuilder: (context, state) => transitionResolver(
            MyCollectionScreen(
                setting: settingRepo),
          ),
        ),

        GoRoute(
          name: 'setting-feedback',
          path: '/setting/feedback',
          pageBuilder: (context, state) => transitionResolver(
            FeedBackScreen(
                setting: settingRepo),
          ),
        ),

        GoRoute(
          name: 'setting-signout',
          path: '/setting/signout',
          pageBuilder: (context, state) => transitionResolver(
            SignoutScreen(
                setting: settingRepo),
          ),
        ),

        GoRoute(
          path: '/home_speeh_chat',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: versionBloc),
                BlocProvider.value(value: ChatBlocManager().getBloc((state.extra as Map)['roomId'])),
              ],
              child: HomeSpeehChatPage(
                setting: settingRepo,
                name: (state.extra as Map)['name'],
                avater: (state.extra as Map)['avater'],
                roomId: (state.extra as Map)['roomId'],
              ),
            ),
          ),
        ),

        GoRoute(
          path: '/gallery_start_create',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: versionBloc),
              ],
              child: GalleryStartCreateScreen(
                setting: settingRepo,
                styleId: (state.extra as Map)['styleId'],
                sizeId: (state.extra as Map)['sizeId'],
                title: (state.extra as Map)['title'],
                content: (state.extra as Map)['content'],
              ),
            ),
          ),
        ),

        GoRoute(
          path: '/user/change-password',
          pageBuilder: (context, state) => transitionResolver(
            ChangePasswordScreen(setting: settingRepo),
          ),
        ),
        GoRoute(
          path: '/user/destroy',
          pageBuilder: (context, state) => transitionResolver(
            DestroyAccountScreen(setting: settingRepo),
          ),
        ),
        GoRoute(
          path: '/signup',
          pageBuilder: (context, state) => transitionResolver(
            SignupScreen(
              settings: settingRepo,
              username: state.queryParameters['username'],
            ),
          ),
        ),
        GoRoute(
          path: '/retrieve-password',
          pageBuilder: (context, state) => transitionResolver(
            RetrievePasswordScreen(
              username: state.queryParameters['username'],
              setting: settingRepo,
            ),
          ),
        ),
        GoRoute(
          name: 'chat_anywhere',
          path: '/chat-anywhere',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(
                  value: ChatBlocManager().getBloc(
                    chatHomeRoomId,
                    chatHistoryId:
                        int.tryParse(state.queryParameters['chat_id'] ?? ''),
                  ),
                ),
                BlocProvider.value(value: chatRoomBloc),
                BlocProvider(create: (context) => NotifyBloc()),
                BlocProvider.value(value: freeCountBloc),
              ],
              child: HomeChatPage(
                stateManager: messageStateManager,
                setting: settingRepo,
                chatId: int.tryParse(state.queryParameters['chat_id'] ?? '0'),
                initialMessage: state.queryParameters['init_message'],
                model: state.queryParameters['model'] == ''
                    ? null
                    : state.queryParameters['model'],
                title: state.queryParameters['title'] == ''
                    ? null
                    : state.queryParameters['title'],
              ),
            ),
          ),
        ),

        GoRoute(
          name: 'chat_chat_history',
          path: '/chat-chat/history',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider(create: (context) => ChatChatBloc(chatMsgRepo)),
              ],
              child: HomeChatHistoryPage(
                setting: settingRepo,
                chatMessageRepo: chatMsgRepo,
              ),
            ),
          ),
        ),
        GoRoute(
          path: '/lab/avatar-selector',
          pageBuilder: (context, state) => transitionResolver(
            const AvatarSelectorScreen(usage: AvatarUsage.room),
          ),
        ),
        GoRoute(
          path: '/lab/draw-board',
          pageBuilder: (context, state) => transitionResolver(
            const DrawboardScreen(),
          ),
        ),

        GoRoute(
          name: 'create-room',
          path: '/create-room',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [BlocProvider.value(value: chatRoomBloc)],
              child: RoomCreatePage(setting: settingRepo),
            ),
          ),
        ),
        GoRoute(
          name: 'home-help',
          path: '/home-help',
          pageBuilder: (context, state) => transitionResolver(
            HomeHelpScreen(setting: settingRepo),
          ),
        ),

        GoRoute(
          name: 'chat',
          path: '/room/:room_id/chat',
          pageBuilder: (context, state) {
            final roomId = int.parse(state.pathParameters['room_id']!);
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider.value(
                    value: ChatBlocManager().getBloc(roomId),
                  ),
                  BlocProvider.value(value: chatRoomBloc),
                  BlocProvider(create: (context) => NotifyBloc()),
                  BlocProvider.value(value: freeCountBloc),
                ],
                child: RoomChatPage(
                  roomId: roomId,
                  titleName: (state.extra as Map)['name'] ?? '助理',
                  avater: (state.extra as Map)['avater'] ?? '',
                  stateManager: messageStateManager,
                  setting: settingRepo,
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'room_setting',
          path: '/room/:room_id/setting',
          pageBuilder: (context, state) {
            final roomId = int.parse(state.pathParameters['room_id']!);
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider.value(value: chatRoomBloc),
                  BlocProvider.value(
                    value: ChatBlocManager().getBloc(roomId),
                  ),
                ],
                child: RoomEditPage(roomId: roomId, setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'account-security-setting',
          path: '/setting/account-security',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: accountBloc),
              ],
              child: AccountSecurityScreen(
                settings: context.read<SettingRepository>(),
              ),
            ),
          ),
        ),
        GoRoute(
          name: 'lab-user-center',
          path: '/lab/user-center',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: accountBloc),
                BlocProvider(
                    create: (context) =>
                        CreativeIslandBloc(creativeIslandRepo)),
              ],
              child:
                  UserCenterScreen(settings: context.read<SettingRepository>()),
            ),
          ),
        ),

        GoRoute(
          name: 'setting-background-selector',
          path: '/setting/background-selector',
          pageBuilder: (context, state) => transitionResolver(
            BlocProvider(
              create: (context) => BackgroundImageBloc(),
              child: BackgroundSelectorScreen(setting: settingRepo),
            ),
          ),
        ),
        GoRoute(
          name: 'setting-openai-custom',
          path: '/setting/openai-custom',
          pageBuilder: (context, state) => transitionResolver(
            OpenAISettingScreen(
              settings: settingRepo,
              source: state.queryParameters['source'],
            ),
          ),
        ),

        GoRoute(
          name: 'creative-upscale',
          path: '/creative-draw/create-upscale',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider(
                    create: (context) =>
                        CreativeIslandBloc(creativeIslandRepo)),
              ],
              child: ImageEditDirectScreen(
                setting: settingRepo,
                title: AppLocale.superResolution.getString(context),
                apiEndpoint: 'upscale',
                note: state.queryParameters['note'],
                initWaitDuration: 15,
                initImage: state.queryParameters['init_image'],
              ),
            ),
          ),
        ),
        GoRoute(
          name: 'creative-colorize',
          path: '/creative-draw/create-colorize',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider(
                    create: (context) =>
                        CreativeIslandBloc(creativeIslandRepo)),
              ],
              child: ImageEditDirectScreen(
                setting: settingRepo,
                title: AppLocale.colorizeImage.getString(context),
                apiEndpoint: 'colorize',
                note: state.queryParameters['note'],
                initWaitDuration: 15,
                initImage: state.queryParameters['init_image'],
              ),
            ),
          ),
        ),
        GoRoute(
          name: 'creative-video',
          path: '/creative-draw/create-video',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider(
                    create: (context) =>
                        CreativeIslandBloc(creativeIslandRepo)),
              ],
              child: ImageEditDirectScreen(
                setting: settingRepo,
                title: '图生视频',
                apiEndpoint: 'image-to-video',
                note: state.queryParameters['note'],
                initWaitDuration: 60,
                initImage: state.queryParameters['init_image'],
              ),
            ),
          ),
        ),

        GoRoute(
          name: 'creative-draw-gallery-preview',
          path: '/creative-draw/gallery/:id',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: galleryBloc),
              ],
              child: GalleryItemScreen(
                setting: settingRepo,
                creativeGallery: state.extra! as CreativeGallery,
                galleryId: int.parse(state.pathParameters['id']!),
              ),
            ),
          ),
        ),

        GoRoute(
          name: 'creative-draw-docreate',
          path: '/creative-draw/docreate',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider(
                    create: (context) =>
                        CreativeIslandBloc(creativeIslandRepo)),
              ],
              child: CreateGenerateScreen(
                  setting: settingRepo,
                  creativeIslandWork: state.extra as CreativeIslandWork),
            ),
          ),
        ),
        GoRoute(
          name: 'creative-draw-create',
          path: '/creative-draw/create',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: galleryBloc),
              ],
              child: DrawCreateScreen(
                setting: settingRepo,
                galleryCopyId: int.tryParse(
                  state.queryParameters['gallery_copy_id'] ?? '',
                ),
                mode: state.queryParameters['mode']!,
                id: state.queryParameters['id']!,
                note: state.queryParameters['note'],
                initImage: state.queryParameters['init_image'],
              ),
            ),
          ),
        ),
        GoRoute(
          name: 'creative-artistic-text',
          path: '/creative-draw/artistic-text',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: galleryBloc),
              ],
              child: ArtisticQRScreen(
                setting: settingRepo,
                galleryCopyId: int.tryParse(
                  state.queryParameters['gallery_copy_id'] ?? '',
                ),
                type: state.queryParameters['type']!,
                id: state.queryParameters['id']!,
                note: state.queryParameters['note'],
              ),
            ),
          ),
        ),
        GoRoute(
          name: 'creative-artistic-wordart',
          path: '/creative-draw/artistic-wordart',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [
                BlocProvider.value(value: galleryBloc),
              ],
              child: ArtisticWordArtScreen(
                setting: settingRepo,
                galleryCopyId: int.tryParse(
                  state.queryParameters['gallery_copy_id'] ?? '',
                ),
                id: state.queryParameters['id']!,
                note: state.queryParameters['note'],
              ),
            ),
          ),
        ),
        GoRoute(
          name: 'creative-island-history-all',
          path: '/creative-island/history',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                      create: (context) =>
                          CreativeIslandBloc(creativeIslandRepo)),
                ],
                child: MyCreationScreen(
                  setting: settingRepo,
                  mode: state.queryParameters['mode'] ?? '',
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'creative-island-models',
          path: '/creative-island/models',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                      create: (context) =>
                          CreativeIslandBloc(creativeIslandRepo)),
                ],
                child: CreativeModelScreen(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'creative-island-history-item',
          path: '/creative-island/:id/history/:item_id',
          pageBuilder: (context, state) {
            final id = state.pathParameters['id']!;
            final itemId = int.tryParse(state.pathParameters['item_id']!);
            final showErrorMessage =
                state.queryParameters['show_error'] == 'true';
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                      create: (context) =>
                          CreativeIslandBloc(creativeIslandRepo)),
                ],
                child: MyCreationItemPage(
                  setting: settingRepo,
                  islandId: id,
                  itemId: itemId!,
                  showErrorMessage: showErrorMessage,
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'quota-details',
          path: '/quota-details',
          pageBuilder: (context, state) => transitionResolver(
            PaymentHistoryScreen(setting: settingRepo),
          ),
        ),
        GoRoute(
          name: 'quota-usage-statistics',
          path: '/quota-usage-statistics',
          pageBuilder: (context, state) => transitionResolver(
            QuotaUsageStatisticsScreen(setting: settingRepo),
          ),
        ),
        GoRoute(
          name: 'quota-usage-daily-details',
          path: '/quota-usage-daily-details',
          pageBuilder: (context, state) => transitionResolver(
            QuotaUsageDetailScreen(
              setting: settingRepo,
              date: state.queryParameters['date'] ??
                  DateFormat('yyyy-MM-dd').format(DateTime.now()),
            ),
          ),
        ),
        GoRoute(
          name: 'prompt-editor',
          path: '/prompt-editor',
          pageBuilder: (context, state) {
            var prompt = state.queryParameters['prompt'] ?? '';
            return transitionResolver(PromptScreen(prompt: prompt));
          },
        ),
        GoRoute(
          name: 'integral',
          path: '/integral',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(create: ((context) => PaymentBloc())),
                ],
                child: GalleryIntegralScreen(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'payment',
          path: '/payment',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(create: ((context) => PaymentBloc())),
                ],
                child: PaymentScreen(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'bind-phone',
          path: '/bind-phone',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider.value(value: accountBloc),
                ],
                child: BindPhoneScreen(
                  setting: settingRepo,
                  isSignIn: state.queryParameters['is_signin'] != 'false',
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'diagnosis',
          path: '/diagnosis',
          pageBuilder: (context, state) => transitionResolver(
            DiagnosisScreen(setting: settingRepo),
          ),
        ),
        GoRoute(
          name: 'free-statistics',
          path: '/free-statistics',
          pageBuilder: (context, state) => transitionResolver(
            MultiBlocProvider(
              providers: [BlocProvider.value(value: freeCountBloc)],
              child: FreeStatisticsPage(setting: settingRepo),
            ),
          ),
        ),
        GoRoute(
          name: 'custom-home-models',
          path: '/setting/custom-home-models',
          pageBuilder: (context, state) => transitionResolver(
            CustomHomeModelsPage(setting: settingRepo),
          ),
        ),
        GoRoute(
          name: 'group-chat-chat',
          path: '/group-chat/:group_id/chat',
          pageBuilder: (context, state) {
            final groupId = int.tryParse(state.pathParameters['group_id']!);

            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: ((context) =>
                        GroupChatBloc(stateManager: messageStateManager)),
                  ),
                  BlocProvider.value(value: chatRoomBloc),
                ],
                child: GroupChatPage(
                  setting: settingRepo,
                  stateManager: messageStateManager,
                  groupId: groupId!,
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'group-chat-create',
          path: '/group-chat-create',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: ((context) =>
                        GroupChatBloc(stateManager: messageStateManager)),
                  ),
                  BlocProvider.value(value: chatRoomBloc),
                ],
                child: GroupCreatePage(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'group-chat-edit',
          path: '/group-chat/:group_id/edit',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: ((context) =>
                        GroupChatBloc(stateManager: messageStateManager)),
                  ),
                  BlocProvider.value(value: chatRoomBloc),
                ],
                child: GroupEditPage(
                  setting: settingRepo,
                  groupId: int.tryParse(state.pathParameters['group_id']!)!,
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'user-api-keys',
          path: '/setting/user-api-keys',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: ((context) => UserApiKeysBloc()),
                  ),
                ],
                child: UserAPIKeysScreen(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'notifications',
          path: '/notifications',
          pageBuilder: (context, state) {
            return transitionResolver(
              NotificationScreen(setting: settingRepo),
            );
          },
        ),
        GoRoute(
          name: 'articles',
          path: '/article',
          pageBuilder: (context, state) {
            return transitionResolver(
              ArticleScreen(
                settings: settingRepo,
                id: int.tryParse(state.queryParameters['id'] ?? '') ?? 0,
              ),
            );
          },
        ),

        GoRoute(
          name: 'web-view-page',
          path: '/web-view-page',
          pageBuilder: (context, state) {
            return transitionResolver(WebViewPage(
              url: state.queryParameters['url'],
              title: state.queryParameters['title'],
              isLocalUrl: state.queryParameters['is_local_url'] == 'true',
            ));
          },
        ),
        /// 管理员接口
        GoRoute(
          name: 'admin-dashboard',
          path: '/admin/dashboard',
          pageBuilder: (context, state) {
            return transitionResolver(
              AdminDashboardPage(setting: settingRepo),
            );
          },
        ),
        GoRoute(
          name: 'admin-models',
          path: '/admin/models',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => ModelBloc(),
                  ),
                ],
                child: AdminModelsPage(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-models-create',
          path: '/admin/models/create',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => ModelBloc(),
                  ),
                ],
                child: AdminModelCreatePage(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-models-edit',
          path: '/admin/models/edit/:id',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => ModelBloc(),
                  ),
                ],
                child: AdminModelEditPage(
                  setting: settingRepo,
                  modelId: state.pathParameters['id']!,
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-channels',
          path: '/admin/channels',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => ChannelBloc(),
                  ),
                ],
                child: ChannelsPage(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-channels-create',
          path: '/admin/channels/create',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => ChannelBloc(),
                  ),
                ],
                child: ChannelAddPage(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-channels-edit',
          path: '/admin/channels/edit/:id',
          pageBuilder: (context, state) {
            final channelId = int.parse(state.pathParameters['id']!);

            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => ChannelBloc(),
                  ),
                ],
                child: ChannelEditPage(
                  setting: settingRepo,
                  channelId: channelId,
                ),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-users',
          path: '/admin/users',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => UserBloc(),
                  ),
                ],
                child: AdminUsersPage(setting: settingRepo),
              ),
            );
          },
        ),
        GoRoute(
          name: 'admin-users-detail',
          path: '/admin/users/:id',
          pageBuilder: (context, state) {
            final userId = int.parse(state.pathParameters['id']!);

            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => UserBloc(),
                  ),
                ],
                child: AdminUserPage(setting: settingRepo, userId: userId),
              ),
            );
          },
        ),

        GoRoute(
          name: 'admin-payment-histories',
          path: '/admin/payment/histories',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => AdminPaymentBloc(),
                  ),
                ],
                child: PaymentHistoriesPage(setting: settingRepo),
              ),
            );
          },
        ),

        GoRoute(
          name: 'admin-user-rooms',
          path: '/admin/users/:id/rooms',
          pageBuilder: (context, state) {
            final userId = int.parse(state.pathParameters['id']!);

            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => AdminRoomBloc(),
                  ),
                ],
                child: AdminRoomsPage(setting: settingRepo, userId: userId),
              ),
            );
          },
        ),

        GoRoute(
          name: 'admin-user-rooms-messages',
          path: '/admin/users/:id/rooms/:room_id/messages',
          pageBuilder: (context, state) {
            final userId = int.parse(state.pathParameters['id']!);
            final roomId = int.parse(state.pathParameters['room_id']!);

            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => AdminRoomBloc(),
                  ),
                ],
                child: AdminRoomMessagesPage(
                  setting: settingRepo,
                  userId: userId,
                  roomId: roomId,
                  roomType: int.parse(state.queryParameters['room_type']!),
                ),
              ),
            );
          },
        ),

        GoRoute(
          name: 'admin-recently-messages',
          path: '/admin/recently-messages',
          pageBuilder: (context, state) {
            return transitionResolver(
              MultiBlocProvider(
                providers: [
                  BlocProvider(
                    create: (context) => AdminRoomBloc(),
                  ),
                ],
                child: AdminRecentlyMessagesPage(setting: settingRepo),
              ),
            );
          },
        ),
      ]);
}
